package com.ncss.http.proxy.server.handler;

import com.ncss.http.proxy.server.CloseUtils;
import com.ncss.http.proxy.server.Parameters;

import javax.rmi.ssl.SslRMIClientSocketFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class DownloadHandler implements Runnable {

    private Parameters parameters;

    public DownloadHandler(Parameters parameters) {
        this.parameters = parameters;
    }

    @Override
    public void run() {
        File file = new CacheImageHandler(this.parameters).handle();
        if (file != null) {
            System.out.println(file.getPath() + "\t" + file.length());
        }
    }


    public static void main(String[] args) {
        //int z = 1;
        //F:\tiles\18\64181
        //F:\darkmap_tiles\13\3413

        int s_x = 0;
        int s_y = 0;
        int s_z = 0;
        int maxLayer = 4;
        //String url = "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/13/3345/6839";
        String url = "https://tiles.windy.com/tiles/v8.1/darkmap/{z}/{x}/{y}.png";
        String filePath = "D:\\darkmap";
        if (args != null) {
            if (args.length != 6) {
                System.err.println("args error.");
            } else {
                s_x = Integer.parseInt(args[0]);
                s_y = Integer.parseInt(args[1]);
                s_z = Integer.parseInt(args[2]);
                maxLayer = Integer.parseInt(args[3]);
                url = args[4];
                filePath = args[5];
            }
        }
        for (int z  = s_z; z <= maxLayer; z++) {
            for (int y = s_y; y <=(Math.pow(2, z)) - 1; y++) {
                for (int x=s_x; x <= (Math.pow(2, z)) - 1; x++) {
                //for (int x=s_x; x <= 7000; x++) {
                //    for (int x=7000; x <= (Math.pow(2, z)) - 1; x++) {
                    try {
                        Parameters parameters = new Parameters(x, y, z);
//                    parameters.setFilePath("F:/tiles");
//                    parameters.setUrl("http://windytiles.mapy.cz/turist-m/{z}-{x}-{y}");


                        //parameters.setFilePath("F:/geoq_tiles");
                        //parameters.setUrl("http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}");


                        parameters.setFilePath(filePath);
                        parameters.setUrl(url);
                        new Thread(new DownloadHandler(parameters)).start();
                        Thread.sleep(10);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        /*
        System.out.println(Math.pow(2, 18));
        for (int z = 10; z <= 18; z++) {
            //int x = (int)(Math.pow(2, z)) - 1;

            //for (int x = 0; x <= (Math.pow(2, z)) - 1; x++) {
            for (int y = 0; y <= (Math.pow(2, z)) - 1; y++) {
                int x = (int) (Math.pow(2, z)) - 1;
                // int y = (int) (Math.pow(2, z)) - 1;
                try {
                    Parameters parameters = new Parameters(x, y, z);
                    new Thread(new DownloadHandler(parameters)).start();
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //}
        }*/
    }


    public static void _main(String[] args) {
        //int z = 1;
        //F:\tiles\18\64181
        //F:\darkmap_tiles\13\3413

        int s_x = 6000;
        int s_y = 3462;
        int s_z = 13;
        int maxLayer = 18;
        //String url = "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/13/3345/6839";
        //String url = "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}";
        String url = "https://tiles.windy.com/tiles/v8.1/darkmap/{z}/{x}/{y}.png";
        String filePath = "F:\\darkmap_tiles";
        if (args != null) {
            if (args.length != 6) {
                System.err.println("args error.");
            } else {
                s_x = Integer.parseInt(args[0]);
                s_y = Integer.parseInt(args[1]);
                s_z = Integer.parseInt(args[2]);
                maxLayer = Integer.parseInt(args[3]);
                url = args[4];
                filePath = args[5];
            }
        }
        for (int z  = s_z; z <= maxLayer; z++) {
            for (int y = s_y; y <= (Math.pow(2, z)) - 1; y++) {
                //for (int x=s_x; x <= (Math.pow(2, z)) - 1; x++) {
                for (int x=s_x; x <= 7000; x++) {
                    try {
                        Parameters parameters = new Parameters(x, y, z);
//                    parameters.setFilePath("F:/tiles");
//                    parameters.setUrl("http://windytiles.mapy.cz/turist-m/{z}-{x}-{y}");


                        //parameters.setFilePath("F:/geoq_tiles");
                        //parameters.setUrl("http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}");


                        parameters.setFilePath(filePath);
                        parameters.setUrl(url);
                        new Thread(new DownloadHandler(parameters)).start();
                        Thread.sleep(10);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        /*
        System.out.println(Math.pow(2, 18));
        for (int z = 10; z <= 18; z++) {
            //int x = (int)(Math.pow(2, z)) - 1;

            //for (int x = 0; x <= (Math.pow(2, z)) - 1; x++) {
            for (int y = 0; y <= (Math.pow(2, z)) - 1; y++) {
                int x = (int) (Math.pow(2, z)) - 1;
                // int y = (int) (Math.pow(2, z)) - 1;
                try {
                    Parameters parameters = new Parameters(x, y, z);
                    new Thread(new DownloadHandler(parameters)).start();
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //}
        }*/
    }
}
